/**
 * Created by Administrator on 2018/7/29/029.
 */
layui.use(['table', 'form', 'layer','element'], function () {

    // 操作对象
    var form = layui.form,
        table = layui.table,
        layer = layui.layer,
        element = layui.element,
        $ = layui.jquery;


    var token = $('#_token').val();
    var tableCate;
    var tableKeyword;
    var index;

    // 初始化
    getCateList();

    //刷新
    $('.btn-refresh').on('click', function () {
        if(index == 0){
            getCateList()
        }else{
            getKeywordList()
        }

    });

    $(".add").on('click',function () {
        if(index == 0){
            layer.open({
                type: 1,
                title:['添加分类','font-size: 16px;color: #000;font-weight: bolder;'],
                skin: 'layui-layer-rim', //加上边框
                area: ['380px', '200px'], //宽高
                content: $('.add_cate'),
                success:function(){

                }
            });
        }else{
            layer.open({
                type: 1,
                title:['添加关键词','font-size: 16px;color: #000;font-weight: bolder;'],
                skin: 'layui-layer-rim', //加上边框
                area: ['380px', '310px'], //宽高
                content: $('.add_keyword'),
                success:function(){

                }
            });
        }

    });

    table.on('tool(cate)',function (obj) {
       if(obj.event === "cate_update"){
           layer.open({
               type: 1,
               title:['修改分类','font-size: 16px;color: #000;font-weight: bolder;'],
               skin: 'layui-layer-rim', //加上边框
               area: ['380px', '200px'], //宽高
               content: $('.up_cate'),
               success:function(){
                   $('#cate_name').val(obj.data.cate_name);
                   $('#cate_id').val(obj.data.id);
               }
           });
       }

    });

    table.on('tool(keyword)',function (obj) {
        if(obj.event === "keyword_update"){

            layer.open({
                type: 1,
                title:['修改关键词','font-size: 16px;color: #000;font-weight: bolder;'],
                skin: 'layui-layer-rim', //加上边框
                area: ['380px', '310px'], //宽高
                content: $('.up_keyword'),
                success:function(){

                    $("#cate_select").val(obj.data.cate_id);
                    $('#up_keyword').val(obj.data.keyword);
                    $('#key_id').val(obj.data.id);
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }

    });

    //tab切换
    element.on('tab(docDemoTabBrief)', function(data){
        index = data.index;
        //分类信息
        if(data.index == 0){
            getCateList()
        }

        //关键词信息
        if(data.index == 1){
            getKeywordList()
        }

    });

    //添加分类提交
    form.on('submit(add_cate)',function(data) {
        var cate = data.field;
        cate._token = token;
        addCate(cate);
    });

    //修改分类提交
    form.on('submit(up_cate)',function(data) {
        var cate = data.field;
        cate._token = token;
        updateCate(cate);
    });

    //添加关键词提交
    form.on('submit(add_key)',function (data) {
       var keyword = data.field;
       keyword._token = token;
       addKeyword(keyword);
    });

    //修改分类提交
    form.on('submit(up_key)',function(data) {
        var cate = data.field;
        cate._token = token;
        updateKeyword(cate);
    });


    //分类列表
    function getCateList() {
        tableCate = table.render({
            elem: '#cate',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'id', title: 'ID'}
                , {field: 'cate_name', title: '分类名称'}
                , {field: 'created_at', title: '创建时间'}
                , {fixed: 'right', title: '操作', width: 160, align: 'center', toolbar: '#cate_tool'} //这里的toolbar值是模板元素的选择器

            ]],
            url: '/get_cate_list',
            where: {
                _token: token
            },    //查询参数
            method: 'get',
            done: function (res, curr, count) {

            }
        });
    }

    //关键词列表
    function getKeywordList() {
        tableKeyword = table.render({
            elem: '#keyword',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'id', title: 'ID'}
                , {field: 'keyword', title: '关键词'}
                , {field: 'cate_name', title: '分类'}
                , {field: 'created_at', title: '创建时间'}
                , {fixed: 'right', title: '操作', width: 160, align: 'center', toolbar: '#keyword_tool'} //这里的toolbar值是模板元素的选择器

            ]],
            url: '/get_keyword_list',
            where: {
                _token: token
            },    //查询参数
            method: 'get',
            done: function (res, curr, count) {

            }
        });
    }

    //添加分类
    function addCate(data) {
        $.ajax({
            url:'/add_cate',
            type:'post',
            data:data,
            timeout: 3000,
            dataType: 'json',
            success: function (data) {
                layer.msg(data.msg,{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getCateList();
                    }
                });
            },
            error: function () {
                layer.msg('出错了',{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getCateList();
                    }
                });
            }


        });
    }

    //添加关键词
    function addKeyword(data) {
        $.ajax({
            url:'/add_keyword',
            type:'post',
            data:data,
            timeout: 3000,
            dataType: 'json',
            success: function (data) {
                layer.msg(data.msg,{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getKeywordList();
                    }
                });
            },
            error: function () {
                layer.msg('出错了',{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getKeywordList();
                    }
                });
            }


        });
    }

    //修改分类
    function updateCate(data) {
        $.ajax({
            url:'/update_cate',
            type:'post',
            data:data,
            timeout: 3000,
            dataType: 'json',
            success: function (data) {
                layer.msg(data.msg,{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getCateList();
                    }
                });
            },
            error: function () {
                layer.msg('出错了',{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getCateList();
                    }
                });
            }


        });
    }

    //修改分类
    function updateKeyword(data) {
        $.ajax({
            url:'/update_keyword',
            type:'post',
            data:data,
            timeout: 3000,
            dataType: 'json',
            success: function (data) {
                layer.msg(data.msg,{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getKeywordList();
                    }
                });
            },
            error: function () {
                layer.msg('出错了',{
                    time: 2000,
                    end:function(){
                        layer.closeAll('page');
                        getKeywordList();
                    }
                });
            }


        });
    }
});